package com.sos.core.game
{
	import com.sos.core.DataObject;

	public class GameCast extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Property names
		protected static const		JSON_PROPERTY_NAME:String				= "name";
		protected static const		JSON_PROPERTY_TEXT:String				= "text";
		protected static const		JSON_PROPERTY_CASTER:String				= "caster";
		protected static const		JSON_PROPERTY_SOURCE_PLAYER:String		= "sourcePlayer";
		protected static const		JSON_PROPERTY_CAST_STATUS:String		= "castStatus";
		protected static const		JSON_PROPERTY_CAST_ERROR:String			= "castError";
		protected static const		JSON_PROPERTY_COST:String				= "cost";
		protected static const		JSON_PROPERTY_PERMANENT:String			= "permanent";
		protected static const		JSON_PROPERTY_EFFECTS:String			= "effect";
		protected static const		JSON_PROPERTY_CARDID:String				= "cardId";
		
		// Default values
		protected static const      DEFAULT_NAME:String						= "";
		protected static const      DEFAULT_TEXT:String						= "";
		protected static const		DEFAULT_CASTER_ID:uint					= DataObject.INVALID_ID;
		protected static const		DEFAULT_CARD_ID:uint					= DataObject.INVALID_ID;
		protected static const		DEFAULT_CASTER_ROLE:GameActionRole		= GameActionRole.NONE;
		protected static const		DEFAULT_STATUS:GameCastStatus			= GameCastStatus.UNKNOWN;
		protected static const		DEFAULT_COST:int						= 0;
		protected static const		DEFAULT_PERMANENT:Boolean				= false;
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var	_name:String;
		private var _cardId:uint;
		private var	_text:String;
		private var	_casterId:uint; 				// card instance id
		private var	_casterRole:GameActionRole;
		private var	_status:GameCastStatus;
		private var	_cost:int;
		private var _permanent:Boolean;
		private var _effects:Vector.<GameCastEffect>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameCast(data:Object = null)
		{
			super();
			// Init objects
			_effects = new Vector.<GameCastEffect>();
			// clears data
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get name():String
		{
			return _name;
		}
		
		public function set name(value:String):void
		{
			_name = value;
		}
		
		public function get text():String
		{
			return _text;
		}
		
		public function set text(value:String):void
		{
			_text = value;
		}
		
		public function get casterId():uint
		{
			return _casterId;
		}
		
		public function set casterId(value:uint):void
		{
			_casterId = value;
		}
		
		public function get casterRole():GameActionRole
		{
			return _casterRole;
		}
		
		public function set casterRole(value:GameActionRole):void
		{
			_casterRole = value;
		}
		
		public function get cost():int
		{
			return _cost;
		}
		
		public function set cost(value:int):void
		{
			_cost = value;
		}
		
		public function get status():GameCastStatus
		{
			return _status;
		}
		
		public function set status(value:GameCastStatus):void
		{
			_status = value;
		}
		
		public function get permanent():Boolean
		{
			return _permanent;
		}
		
		public function set permanent(value:Boolean):void
		{
			_permanent = value;
		}
		
		public function get effects():Vector.<GameCastEffect>
		{
			return _effects;
		}
		
		public function get cardId():uint 
		{
			return _cardId;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		override protected function validate(data:Object):void
		{
			// Base properties
			if (!data.hasOwnProperty(JSON_PROPERTY_NAME)) 			throwLoadMissingPropError(JSON_PROPERTY_NAME);
			if (!data.hasOwnProperty(JSON_PROPERTY_TEXT)) 			throwLoadMissingPropError(JSON_PROPERTY_TEXT);
			if (!data.hasOwnProperty(JSON_PROPERTY_CASTER)) 		throwLoadMissingPropError(JSON_PROPERTY_CASTER);
			if (!data.hasOwnProperty(JSON_PROPERTY_SOURCE_PLAYER)) 	throwLoadMissingPropError(JSON_PROPERTY_SOURCE_PLAYER);
			if (!data.hasOwnProperty(JSON_PROPERTY_CAST_STATUS)) 	throwLoadMissingPropError(JSON_PROPERTY_CAST_STATUS);
			if (!data.hasOwnProperty(JSON_PROPERTY_CARDID)) 		throwLoadMissingPropError(JSON_PROPERTY_CARDID);
		}
		
		override protected function fill(data:Object):void
		{			
			// Cast status
			var status:int = int(data[JSON_PROPERTY_CAST_STATUS]);
			if (GameCastStatus.PLAYED.id == status)
			{
				_status = GameCastStatus.PLAYED;
			}
			else
			{
				_status = GameCastStatus.getByName(data[JSON_PROPERTY_CAST_ERROR]);
			}
			// Mandatory properties
			_name			= data[JSON_PROPERTY_NAME];
			_text			= data[JSON_PROPERTY_TEXT];
			_casterRole		= GameActionRole.getBySign(data[JSON_PROPERTY_CASTER]);
			_casterId		= uint(data[JSON_PROPERTY_SOURCE_PLAYER]);
			_cardId			= uint(data[JSON_PROPERTY_CARDID]);
			// Optional properties
			if (data.hasOwnProperty(JSON_PROPERTY_COST))
			{
				_cost = int(data[JSON_PROPERTY_COST]);
			}
			if (data.hasOwnProperty(JSON_PROPERTY_PERMANENT))
			{
				_permanent = Boolean(data[JSON_PROPERTY_PERMANENT]);
			}
			// Temporary effects list
			var effects:Vector.<GameCastEffect> = new Vector.<GameCastEffect>;
			// Check if JSON_PROPERTY_EFFECTS exists 
			if (data.hasOwnProperty(JSON_PROPERTY_EFFECTS))
			{
				// Loop over raw effects
				for (var effect:String in data[JSON_PROPERTY_EFFECTS])
				{
					// Loop over effects targets array
					var raw_effect_targets:Array = data[JSON_PROPERTY_EFFECTS][effect];
					for (var j:int = 0; j<raw_effect_targets.length; j++)
					{
						effects.push(new GameCastEffect(effect, raw_effect_targets[j]));
					}
				}
			}
			// Clear and popolate effects list
			_effects.splice(0, _effects.length);
			for (var i:int = 0; i < effects.length; i++)
			{
				_effects.push(effects[i]);
			}
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			// Clear data
			_name		= DEFAULT_NAME;
			_cardId		= DEFAULT_CARD_ID;
			_text		= DEFAULT_TEXT;
			_casterId	= DEFAULT_CASTER_ID;
			_casterRole	= DEFAULT_CASTER_ROLE;
			_status		= DEFAULT_STATUS;
			_cost		= DEFAULT_COST;
			_permanent	= DEFAULT_PERMANENT;
			// Clear objects
			_effects.splice(0, _effects.length);
		}


	}
}